package com.revzone.demo001.PermissionModule.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.revzone.demo001.PermissionModule.mapper.UserMapper;
import com.revzone.demo001.entity.OtherEntity.R;
import com.revzone.demo001.entity.PermissionEntity.User;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * admin角色权限controller
 */
@RestController
@Api(tags = "角色admin管理", description = "用于角色admin管理的相关API")
@RequestMapping("/admin")
public class AdminController {
    private final UserMapper userMapper;

    @Autowired
    public AdminController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @GetMapping("/getUser")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getUser() {
        HashMap<String, Object> map = new HashMap<>();
//        List<String> list = userMapper.getUser();
        List<User> list = userMapper.selectList(null);
        map.put("getUser",list);
        return R.ok(map);
    }

    /**
     * 封号操作
     */
    @PostMapping("/banUser")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R updatePassword(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        user.setStatus(0);
        userMapper.update(user,wrapper);
        return R.ok("成功封号！");
    }
}
